package com.lh.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lh.entity.table.CompanyData;
import com.lh.entity.table.EnterpriseData;
import com.lh.mapper.EnterpriseDataMapper;
import com.lh.service.EnterpriseDataService;
import com.lh.utils.Answer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class EnterpriseDataServiceImpl implements EnterpriseDataService {

    @Autowired
    private EnterpriseDataMapper EnterpriseDataMapper;

    @Override
    public Answer<Object> addEnterpriseData(EnterpriseData EnterpriseData) {
        try{
            EnterpriseData.setCreatedTime(new Date());
            EnterpriseDataMapper.insert(EnterpriseData);
            return Answer.ok("添加成功");
        }catch (Exception e){
            e.printStackTrace();
            return Answer.error("添加失败");
        }
    }

    @Override
    public Answer<Object> updateEnterpriseData(EnterpriseData EnterpriseData) {
        try{
            Integer id = EnterpriseData.getId();
            if (id == null){
                return Answer.error("id不能为空");
            }
            EnterpriseDataMapper.updateById(EnterpriseData);
            return Answer.ok("更新成功");
        }catch (Exception e){
            e.printStackTrace();
            return Answer.error("更新失败");
        }
    }

    @Override
    public Answer<Object> deleteEnterpriseData(EnterpriseData EnterpriseData) {
        try{
            Integer id = EnterpriseData.getId();
            if (id == null){
                return Answer.error("id不能为空");
            }
            EnterpriseDataMapper.deleteById(id);
            return Answer.ok("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return Answer.error("删除失败");
        }
    }

    @Override
    public Answer<Object> queryEnterpriseDataList(EnterpriseData EnterpriseData) {
        try{
            LambdaQueryWrapper<EnterpriseData> EnterpriseDataLambdaQueryWrapper = new LambdaQueryWrapper<>();


            List<EnterpriseData> list = EnterpriseDataMapper.selectList(EnterpriseDataLambdaQueryWrapper);
            return Answer.ok(list);
        }catch (Exception e){
            e.printStackTrace();
            return Answer.error("查询失败");
        }
    }

    @Override
    public Answer<Object> queryEnterpriseDataPage(JSONObject reqJson) {
        try {
            Integer pageSize = reqJson.getInteger("pageSize");
            Integer pageNum = reqJson.getInteger("pageNum");
            Page<EnterpriseData> page = new Page<>(pageNum, pageSize);
            LambdaQueryWrapper<EnterpriseData> EnterpriseDataLambdaQueryWrapper = new LambdaQueryWrapper<>();
            String departmentName = reqJson.getString("departmentName");
            String personResponsible = reqJson.getString("personResponsible");
//            String problem = reqJson.getString("problem");
            String lampStatus = reqJson.getString("lampStatus");
//            String business = reqJson.getString("business");
//
//
            if (StrUtil.isNotBlank(departmentName)){
                EnterpriseDataLambdaQueryWrapper.eq(EnterpriseData::getDepartmentName,departmentName);
            }
            if (StrUtil.isNotBlank(personResponsible)){
                EnterpriseDataLambdaQueryWrapper.like(EnterpriseData::getPersonResponsible,personResponsible);
            }
//
//            if (StrUtil.isNotBlank(problem)){
//                EnterpriseDataLambdaQueryWrapper.like(EnterpriseData::getProblem,problem);
//            }
            if (StrUtil.isNotBlank(lampStatus)){
                EnterpriseDataLambdaQueryWrapper.eq(EnterpriseData::getLampStatus,lampStatus);
            }
//            if (StrUtil.isNotBlank(business)){
//                EnterpriseDataLambdaQueryWrapper.eq(EnterpriseData::getBusiness,business);
//            }

            Page<EnterpriseData> EnterpriseDataPage = EnterpriseDataMapper.selectPage(page, EnterpriseDataLambdaQueryWrapper);
            return Answer.ok(EnterpriseDataPage);
        }catch (Exception e){
            e.printStackTrace();
            return Answer.error("查询失败");
        }
    }

    @Override
    public Answer<Object> personResponsibleList() {
        List<EnterpriseData> enterpriseData = EnterpriseDataMapper.selectList(new LambdaQueryWrapper<>());
        List<String> personResponsibleList = enterpriseData.stream()
                .map(EnterpriseData::getPersonResponsible)
                .filter(Objects::nonNull)  // 过滤null值
                .flatMap(person -> {
                    // 按空格分割字符串，生成新的流
                    String[] parts = person.split(" ");
                    List<String> result = new ArrayList<>();
                    for (String part : parts) {
                        // 过滤掉分割后可能产生的空字符串
                        if (!part.trim().isEmpty()) {
                            result.add(part.trim());
                        }
                    }
                    return result.stream();
                })
                .distinct()  // 去重操作
                .collect(Collectors.toList());
        return Answer.ok(personResponsibleList);
    }

    @Override
    public Answer<Object> departmentName() {
        List<EnterpriseData> enterpriseData = EnterpriseDataMapper.selectList(new LambdaQueryWrapper<>());
        List<String> departmentNameList = enterpriseData.stream()
                .map(EnterpriseData::getDepartmentName)
                .filter(Objects::nonNull)  // 过滤null值
                .flatMap(person -> {
                    // 按空格分割字符串，生成新的流
                    String[] parts = person.split(" ");
                    List<String> result = new ArrayList<>();
                    for (String part : parts) {
                        // 过滤掉分割后可能产生的空字符串
                        if (!part.trim().isEmpty()) {
                            result.add(part.trim());
                        }
                    }
                    return result.stream();
                })
                .distinct()  // 去重操作
                .collect(Collectors.toList());
        return Answer.ok(departmentNameList);
    }
}
